package com.mrx.java8.stream.exercise;

import com.mrx.java8.stream.Employee;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 练习题
 * @create 2018-12-01 11:02
 */
public class TestStreamApi {

    /**
     * 1、给定一个数字列表，返回一个由数的平方构成的列表
     * 给定[1,2,3,4,5]，应返回[1,4,9,16,25]
     */
    @Test
    public void test1(){
        List<Integer> list = Arrays.asList(1,2,3,4,5);
        List<Integer> list2 = list.stream()
                .map(x-> x*x)
                .collect(Collectors.toList());
        System.out.println(list2);
    }
    /**
     *2、用map和reduce方法数一数流中有多少个Employee
     */
    List<Employee> employees = Arrays.asList(
            new Employee("张三", 18, 9999.99,Employee.Status.FREE),
            new Employee("李四", 38, 5555.55,Employee.Status.BUSY),
            new Employee("王五", 50, 6666.66,Employee.Status.VOCATION),
            new Employee("赵六", 16, 3333.33,Employee.Status.FREE),
            new Employee("田七", 8, 7777.77,Employee.Status.BUSY)
    );
    @Test
    public void test2(){
        Optional<Integer> op1 =  employees.stream()
                .map(e->1)
                .reduce(Integer::sum);
        System.out.println(op1.get());
    }

}
